package com.siss.tenancy.finals;

/**
 * 类名: TenantContextHolder.java
 * 描述: 静态的本地线程变量来存储多租户(数据库、redis、文件存储)信息
 * 创建人: yingw
 * 创建时间: 2018年7月5日上午9:05:44
 */
public class TenantContextHolder {
	private static ThreadLocal<String> dbSchemaThreadLocal = new ThreadLocal<String>();
	private static ThreadLocal<Integer> dbRedisIndexThreadLocal = new ThreadLocal<Integer>();
	private static ThreadLocal<String> filePathThreadLocal = new ThreadLocal<String>();

    public static final void setSchema(String schema) {
    	dbSchemaThreadLocal.set(schema);
    }

    public static final String getSchema() {
        String schema = dbSchemaThreadLocal.get();
        /*if (schema == null) {
        	schema = "siss_basic_sz";
        }*/
        return schema;
    }
    
    public static final void setRedisIndex(Integer redisIndex) {
    	dbRedisIndexThreadLocal.set(redisIndex);
    }

    public static final Integer getRedisIndex() {
    	Integer redisIndex = dbRedisIndexThreadLocal.get();
        /*if (redisIndex == null) {
        	redisIndex = 0;
        }*/
        return redisIndex;
    }
    
    public static final void setFilePath(String filePath) {
    	filePathThreadLocal.set(filePath);
    }

    public static final String getFilePath() {
        String filePath = filePathThreadLocal.get();
        if (filePath == null) {
        	filePath = "group1";
        }
        return filePath;
    }

    public static final void remove() {
    	dbSchemaThreadLocal.remove();
    	dbRedisIndexThreadLocal.remove();
    	filePathThreadLocal.remove();
    }
    
    public static final void removeRedis() {
    	dbRedisIndexThreadLocal.remove();
    }
}
